package Servlet;

import DAO.ComplaintDAO;
import javabean.Complaint;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/CompleteComplaint")
public class CompleteComplaintServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        ComplaintDAO complaintDAO = (ComplaintDAO) DAO.DAOFactory.getComplaintDAOInstance();
        int ComplaintID = Integer.parseInt(req.getParameter("ComplaintID"));
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html; charset=UTF-8");
        try {
            Complaint complaint = complaintDAO.getComplaintByID(ComplaintID);
            complaint.setComplaintStatus("已完成");
            if(complaintDAO.updateComplaint(complaint)){
                out.print("<script>alert('操作成功!');window.location.href='UnassignedComplaint'</script>");
            }else{
                out.print("<script>alert('操作失败!');window.location.href='UnassignedComplaint'</script>");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
